<!doctype html>



  


<html class="theme-next muse use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  
    
      
    

    
  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Microsoft YaHei:300,300italic,400,400italic,700,700italic|consolas:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Node.js,http," />








  <link rel="shortcut icon" type="image/x-icon" href="http://ofy7k3v4s.bkt.clouddn.com/mylogo.png?v=5.0.2" />






<meta name="description" content="&amp;#x8D85;&amp;#x6587;&amp;#x672C;&amp;#x4F20;&amp;#x8F93;&amp;#x534F;&amp;#x8BAE;&amp;#xFF08;HTTP&amp;#xFF0C;HyperText Transfer Protocol)&amp;#x662F;&amp;#x4E92;&amp;#x8054;&amp;#x7F51;&amp;#x4E0A;&amp;#x5E94;&amp;#x7528;&amp;#x6700;&amp;#x4E3A;&amp;#x5E7F;&amp;#x6CDB;&amp;#x7684;">
<meta property="og:type" content="article">
<meta property="og:title" content="Node-http-网站">
<meta property="og:url" content="https://hongqinma.github.io/2016/10/09/s007-node-http-网站/index.html">
<meta property="og:site_name" content="HongqinMa' Blog">
<meta property="og:description" content="&amp;#x8D85;&amp;#x6587;&amp;#x672C;&amp;#x4F20;&amp;#x8F93;&amp;#x534F;&amp;#x8BAE;&amp;#xFF08;HTTP&amp;#xFF0C;HyperText Transfer Protocol)&amp;#x662F;&amp;#x4E92;&amp;#x8054;&amp;#x7F51;&amp;#x4E0A;&amp;#x5E94;&amp;#x7528;&amp;#x6700;&amp;#x4E3A;&amp;#x5E7F;&amp;#x6CDB;&amp;#x7684;">
<meta property="og:updated_time" content="2016-11-30T11:41:08.608Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Node-http-网站">
<meta name="twitter:description" content="&amp;#x8D85;&amp;#x6587;&amp;#x672C;&amp;#x4F20;&amp;#x8F93;&amp;#x534F;&amp;#x8BAE;&amp;#xFF08;HTTP&amp;#xFF0C;HyperText Transfer Protocol)&amp;#x662F;&amp;#x4E92;&amp;#x8054;&amp;#x7F51;&amp;#x4E0A;&amp;#x5E94;&amp;#x7528;&amp;#x6700;&amp;#x4E3A;&amp;#x5E7F;&amp;#x6CDB;&amp;#x7684;">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '6347850919329138000',
      author: 'Hiraku'
    }
  };
</script>




  <link rel="canonical" href="https://hongqinma.github.io/2016/10/09/s007-node-http-网站/"/>


  <title> Node-http-网站 | HongqinMa' Blog </title>
</head>

<body itemscope itemtype="//schema.org/WebPage" lang="en">

  



  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?10010323";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>








  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="//schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta custom-logo">
  
    <div class="site-meta-headline">
      <a>
        <img class="custom-logo-image" src="http://ofy7k3v4s.bkt.clouddn.com/photo.jpg"
             alt="HongqinMa' Blog"/>
      </a>
    </div>
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">HongqinMa' Blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">Spell at all costs， to rush you in the future。</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            About
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            Tags
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="//schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Node-http-网站
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">Posted on</span>
            <time itemprop="dateCreated" datetime="2016-10-09T23:59:36+08:00" content="2016-10-09">
              2016-10-09
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">In</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/Node-js/" itemprop="url" rel="index">
                    <span itemprop="name">Node.js</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/2016/10/09/s007-node-http-网站/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2016/10/09/s007-node-http-网站/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          

          
              &nbsp; | &nbsp;
              <span class="page-pv"><i class="fa fa-file-o"></i>
              <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
              </span>
          

          
            <span id="busuanzi_container_page_pv">  <span id="busuanzi_value_page_pv"></span> Views</span>
          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>&#x8D85;&#x6587;&#x672C;&#x4F20;&#x8F93;&#x534F;&#x8BAE;&#xFF08;HTTP&#xFF0C;HyperText Transfer Protocol)&#x662F;&#x4E92;&#x8054;&#x7F51;&#x4E0A;&#x5E94;&#x7528;&#x6700;&#x4E3A;&#x5E7F;&#x6CDB;&#x7684;&#x4E00;&#x79CD;&#x7F51;&#x7EDC;&#x534F;&#x8BAE;</p>
<ul>
<li>&#x63A5;&#x6536;&#x8BF7;&#x6C42;</li>
<li>&#x5904;&#x7406;&#x8BF7;&#x6C42;</li>
<li>&#x53D1;&#x9001;&#x54CD;&#x5E94;</li>
</ul>
<p>net &#x4E3A;&#x4EC0;&#x4E48;&#x4E0D;&#x80FD;&#x5904;&#x7406;&#x6D4F;&#x89C8;&#x5668;&#x7684;&#x54CD;&#x5E94;&#x5462;&#xFF1F;</p>
<p>http &#x6A21;&#x5757;&#x548C; net &#x6A21;&#x5757;&#x4E4B;&#x95F4;&#x7684;&#x533A;&#x522B;</p>
<h2 id="http-&#x6A21;&#x5757;&#x7684;&#x7B80;&#x5355;&#x4F7F;&#x7528;"><a href="#http-&#x6A21;&#x5757;&#x7684;&#x7B80;&#x5355;&#x4F7F;&#x7528;" class="headerlink" title="http &#x6A21;&#x5757;&#x7684;&#x7B80;&#x5355;&#x4F7F;&#x7528;"></a>http &#x6A21;&#x5757;&#x7684;&#x7B80;&#x5355;&#x4F7F;&#x7528;</h2><p>(1). &#x521B;&#x5EFA;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x5F97;&#x5230;&#x4E00;&#x4E2A; Server &#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;</p>
<ul>
<li>&#x4EFB;&#x4F55;&#x8BF7;&#x6C42;&#x90FD;&#x4F1A;&#x89E6;&#x53D1;&#x8BE5; request &#x8BF7;&#x6C42;&#x4E8B;&#x4EF6;&#xFF0C;&#x7136;&#x540E;&#x6267;&#x884C;&#x4E8B;&#x4EF6;&#x5904;&#x7406;&#x51FD;&#x6570;</li>
<li>&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x6240;&#x6709;&#x7684;&#x8BF7;&#x6C42;&#x5165;&#x53E3;&#x5C31;&#x662F;&#x8FD9;&#x4E2A; request &#x4E8B;&#x4EF6;</li>
<li>&#x5982;&#x4F55;&#x533A;&#x5206;&#x4E0D;&#x540C;&#x7684;&#x8BF7;&#x6C42;<ul>
<li>&#x6BCF;&#x4E2A;&#x8BF7;&#x6C42;&#x6709;&#x8BF7;&#x6C42;&#x62A5;&#x6587;: &#x8BF7;&#x6C42;&#x5934;&#x3001;&#x8BF7;&#x6C42;&#x8DEF;&#x5F84;&#x3001;&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;&#x7B49;&#x4FE1;&#x606F;</li>
</ul>
</li>
<li>Node &#x5C06;&#x6BCF;&#x4E00;&#x4E2A;&#x8BF7;&#x6C42;&#x4E2D;&#x7684;&#x8BF7;&#x6C42;&#x62A5;&#x6587;&#x4FE1;&#x606F;&#x89E3;&#x6790;&#x4E3A;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF1A;Request &#xFF0C;&#x6302;&#x8F7D;&#x7ED9;&#x8BF7;&#x6C42;&#x5904;&#x7406;&#x51FD;&#x6570;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;<ul>
<li>&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; Request &#x8BF7;&#x6C42;&#x5BF9;&#x8C61;&#x62FF;&#x5230;&#x4E00;&#x4E9B;&#x8BF7;&#x6C42;&#x62A5;&#x6587;&#x4FE1;&#x606F;&#xFF0C;&#x4F8B;&#x5982;&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;&#x3001;&#x8BF7;&#x6C42;&#x8DEF;&#x5F84;&#x3001;&#x8BF7;&#x6C42;&#x5934;&#x90E8;&#x5B57;&#x6BB5;&#x7B49;&#x4FE1;&#x606F;</li>
</ul>
</li>
<li>&#x540C;&#x65F6;&#xFF0C;Node &#x8FD8;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;&#x63A5;&#x53E3;&#x5BF9;&#x8C61;&#xFF1A;Response<ul>
<li>&#x8BE5;&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x7ED9;&#x5F53;&#x524D;&#x8BF7;&#x6C42;&#x53D1;&#x9001;&#x54CD;&#x5E94;&#x6570;&#x636E;</li>
</ul>
</li>
</ul>
<p>(2). &#x76D1;&#x542C;&#x670D;&#x52A1;&#x5668; Server &#x5BF9;&#x8C61;&#x7684; Request &#x8BF7;&#x6C42;&#x4E8B;&#x4EF6;&#xFF0C;&#x8BBE;&#x7F6E;&#x8BF7;&#x6C42;&#x5904;&#x7406;&#x51FD;&#x6570;</p>
<p>(3). &#x7ED1;&#x5B9A;&#x76D1;&#x542C;&#x7AEF;&#x53E3;&#xFF0C;&#x542F;&#x52A8;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x8BBE;&#x7F6E;&#x542F;&#x52A8;&#x6210;&#x529F;&#x4E4B;&#x540E;&#x7684;&#x56DE;&#x8C03;&#x5904;&#x7406;&#x51FD;&#x6570;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// &#x5BFC;&#x5165; http &#x6A21;&#x5757;</span></div><div class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">&apos;http&apos;</span>);</div><div class="line"><span class="comment">// &#x521B;&#x5EFA; http &#x670D;&#x52A1;&#x5668;</span></div><div class="line"><span class="keyword">const</span> server = http.createServer(); </div><div class="line"><span class="comment">// &#x76D1;&#x542C; request &#x4E8B;&#x4EF6;</span></div><div class="line">server.on(<span class="string">&apos;request&apos;</span>, (req, res) =&gt; {</div><div class="line">  res.end(<span class="string">&apos;hello world&apos;</span>);</div><div class="line">});</div><div class="line"><span class="comment">// &#x5F00;&#x542F;&#x670D;&#x52A1;&#x7AEF;&#x53E3;</span></div><div class="line">server.listen(<span class="number">3000</span>, () =&gt; {</div><div class="line">  <span class="built_in">console</span>.log(<span class="string">&apos;Server is running at port 3000.&apos;</span>);</div><div class="line">});</div></pre></td></tr></table></figure>
<h2 id="&#x5BF9;&#x4E0D;&#x540C;&#x7684;&#x8BF7;&#x6C42;&#x53D1;&#x9001;&#x4E0D;&#x540C;&#x7684;&#x54CD;&#x5E94;"><a href="#&#x5BF9;&#x4E0D;&#x540C;&#x7684;&#x8BF7;&#x6C42;&#x53D1;&#x9001;&#x4E0D;&#x540C;&#x7684;&#x54CD;&#x5E94;" class="headerlink" title="&#x5BF9;&#x4E0D;&#x540C;&#x7684;&#x8BF7;&#x6C42;&#x53D1;&#x9001;&#x4E0D;&#x540C;&#x7684;&#x54CD;&#x5E94;"></a>&#x5BF9;&#x4E0D;&#x540C;&#x7684;&#x8BF7;&#x6C42;&#x53D1;&#x9001;&#x4E0D;&#x540C;&#x7684;&#x54CD;&#x5E94;</h2><ul>
<li>&#x5F53;&#x7528;&#x6237;&#x8BBF;&#x95EE; <code>&apos;/&apos;</code> &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8FD4;&#x56DE; index page</li>
<li>&#x5F53;&#x7528;&#x6237;&#x8BBF;&#x95EE; <code>&apos;/add&apos;</code> &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8FD4;&#x56DE; add page</li>
<li>&#x5F53;&#x7528;&#x6237;&#x8BBF;&#x95EE; <code>&apos;/about&apos;</code> &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8FD4;&#x56DE; about page</li>
<li>&#x5F53;&#x7528;&#x6237;&#x8BBF;&#x95EE; <code>&apos;/xxx&apos;</code> &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8FD4;&#x56DE; 404</li>
</ul>
<p>(1). &#x83B7;&#x53D6;&#x5F53;&#x524D;&#x7684;&#x8BF7;&#x6C42;&#x8DEF;&#x5F84;&#xFF08;&#x901A;&#x8FC7; Request &#x8BF7;&#x6C42;&#x5BF9;&#x8C61;&#x7684; url &#x5C5E;&#x6027;&#x83B7;&#x53D6;&#xFF09;</p>
<p><strong>&#x8FD9;&#x91CC;&#x7684;&#x8BF7;&#x6C42;&#x8DEF;&#x5F84;&#x6C38;&#x8FDC;&#x90FD;&#x662F;&#x4EE5;  <code>/</code> &#x5F00;&#x5934;&#x7684;</strong></p>
<p>&#x4F8B;&#x5982;&#x4F60;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x5730;&#x5740;&#x4E2D;&#x8F93;&#x5165;&#x7684;&#x662F;:</p>
<ul>
<li><code>http://127.0.0.1:3000</code> &#x5219; url &#x5C31;&#x662F; /</li>
<li><code>http://127.0.0.1:3000/</code>  &#x5219; url &#x5C31;&#x662F; /</li>
<li><code>http://127.0.0.1:3000/add</code>  &#x5219; url &#x5C31;&#x662F; /add</li>
</ul>
<p>(2). &#x53D1;&#x9001;&#x54CD;&#x5E94;</p>
<p>2.1 &#x8BF7;&#x6C42;&#x4E4B;&#x540E;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; res.write &#x65B9;&#x6CD5;&#x53D1;&#x9001;&#x54CD;&#x5E94;</p>
<p>2.2 &#x6CE8;&#x610F;&#xFF1A;res.write &#x53EF;&#x4EE5;&#x5411;&#x54CD;&#x5E94;&#x6D41;&#x4E2D;&#x591A;&#x6B21;&#x53D1;&#x9001;&#x6570;&#x636E;&#xFF0C;</p>
<ul>
<li>&#x4F46;&#x662F;&#x4E00;&#x5B9A;&#x8981;&#x5728;&#x5199;&#x5B8C;&#x54CD;&#x5E94;&#x6D41;&#x6570;&#x636E;&#x4E4B;&#x540E;&#x8C03;&#x7528; <code>res.end()</code> &#x65B9;&#x6CD5;&#x7ED3;&#x675F;&#x54CD;&#x5E94;&#x3002;</li>
<li>&#x5426;&#x5219;&#x5BA2;&#x6237;&#x7AEF;&#x6D4F;&#x89C8;&#x5668;&#x8FD8;&#x8BA4;&#x4E3A;&#x4F60;&#x7684;&#x6570;&#x636E;&#x6CA1;&#x6709;&#x53D1;&#x9001;&#x5B8C;&#x6BD5;&#xFF0C;&#x4E00;&#x76F4;&#x7B49;&#x5F85;&#x63A5;&#x6536;&#x3002;</li>
</ul>
<p>2.3 &#x4E00;&#x822C;&#x53D1;&#x9001;&#x54CD;&#x5E94;&#x6570;&#x636E;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5F88;&#x5C11;&#x6709;&#x8FD9;&#x79CD;&#x9700;&#x8981;&#x591A;&#x6B21;&#x8C03;&#x7528; write &#x65B9;&#x6CD5;&#x6765;&#x53D1;&#x9001;&#x7684;&#x6570;&#x636E;</p>
<ul>
<li>&#x5C31;&#x662F;&#x8BF4;&#x4E00;&#x822C;&#x5C31;&#x662F; <code>res.write(&apos;&#x54CD;&#x5E94;&#x6570;&#x636E;&apos;)</code>, <code>res.end()</code> &#x7ED3;&#x675F;&#x54CD;&#x5E94;</li>
<li>&#x6240;&#x4EE5;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>res.end(&apos;&#x54CD;&#x5E94;&#x6570;&#x636E;&apos;)</code> &#x76F4;&#x63A5;&#x53D1;&#x9001;&#x54CD;&#x5E94;&#x6570;&#x636E;&#xFF0C;&#x540C;&#x65F6;&#x7ED3;&#x675F;&#x54CD;&#x5E94;</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">&apos;http&apos;</span>);</div><div class="line"><span class="keyword">const</span> server = http.createServer();</div><div class="line">server.on(<span class="string">&apos;request&apos;</span>, (req, res) =&gt; {</div><div class="line">  <span class="keyword">const</span> url = req.url;   </div><div class="line">  <span class="comment">// res.write(url);  </span></div><div class="line">  <span class="comment">// res.end();</span></div><div class="line">  res.end(url);</div><div class="line">});</div><div class="line">server.listen(<span class="number">3000</span>, () =&gt; {</div><div class="line">  <span class="built_in">console</span>.log(<span class="string">&apos;Server is running at port 3000...&apos;</span>);</div><div class="line">});</div></pre></td></tr></table></figure>
<h2 id="&#x6839;&#x636E;&#x4E0D;&#x540C;&#x8BF7;&#x6C42;&#x54CD;&#x5E94;&#x4E0D;&#x540C;&#x5185;&#x5BB9;"><a href="#&#x6839;&#x636E;&#x4E0D;&#x540C;&#x8BF7;&#x6C42;&#x54CD;&#x5E94;&#x4E0D;&#x540C;&#x5185;&#x5BB9;" class="headerlink" title="&#x6839;&#x636E;&#x4E0D;&#x540C;&#x8BF7;&#x6C42;&#x54CD;&#x5E94;&#x4E0D;&#x540C;&#x5185;&#x5BB9;"></a>&#x6839;&#x636E;&#x4E0D;&#x540C;&#x8BF7;&#x6C42;&#x54CD;&#x5E94;&#x4E0D;&#x540C;&#x5185;&#x5BB9;</h2><p>&#x56E0;&#x6B64;&#xFF0C;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x6765;&#x5224;&#x65AD;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">&apos;http&apos;</span>);</div><div class="line"><span class="keyword">const</span> server = http.createServer();</div><div class="line">server.on(<span class="string">&apos;request&apos;</span>, (req, res) =&gt; {</div><div class="line">  <span class="keyword">const</span> url = req.url;   </div><div class="line">  <span class="keyword">if</span> (url === <span class="string">&apos;/&apos;</span>) {</div><div class="line">    res.end(<span class="string">&apos;&lt;h1&gt;index page&lt;/h1&gt;&apos;</span>);</div><div class="line">  } <span class="keyword">else</span> <span class="keyword">if</span> (url === <span class="string">&apos;/add&apos;</span>) {</div><div class="line">    res.end(<span class="string">&apos;&lt;h1&gt;add page&lt;/h1&gt;&apos;</span>);</div><div class="line">  } <span class="keyword">else</span> <span class="keyword">if</span> (url === <span class="string">&apos;/about&apos;</span>) {</div><div class="line">    res.end(<span class="string">&apos;about page&apos;</span>);</div><div class="line">  } <span class="keyword">else</span> {</div><div class="line">    res.end(<span class="string">&apos;404 Not Found.&apos;</span>);</div><div class="line">  }</div><div class="line">});</div><div class="line">server.listen(<span class="number">3000</span>, () =&gt; {</div><div class="line">  <span class="built_in">console</span>.log(<span class="string">&apos;Server is running at port 3000...&apos;</span>);</div><div class="line">});</div></pre></td></tr></table></figure>
<h2 id="&#x5904;&#x7406;&#x9875;&#x9762;&#x4E2D;&#x7684;&#x9759;&#x6001;&#x8D44;&#x6E90;"><a href="#&#x5904;&#x7406;&#x9875;&#x9762;&#x4E2D;&#x7684;&#x9759;&#x6001;&#x8D44;&#x6E90;" class="headerlink" title="&#x5904;&#x7406;&#x9875;&#x9762;&#x4E2D;&#x7684;&#x9759;&#x6001;&#x8D44;&#x6E90;"></a>&#x5904;&#x7406;&#x9875;&#x9762;&#x4E2D;&#x7684;&#x9759;&#x6001;&#x8D44;&#x6E90;</h2><p>&#x4E0A;&#x9762;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x8868;&#x793A;&#x53EF;&#x4EE5;&#x89E3;&#x6790; html &#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x90A3;&#x4E5F;&#x53EF;&#x4EE5;&#x54CD;&#x5E94;&#x9875;&#x9762;&#x3002;</p>
<p>&#x5982;&#x679C;&#x8981;&#x5904;&#x7406;&#x4E00;&#x4E9B;&#x8BFB;&#x53D6;&#x51FA;&#x6765;&#x7684; html &#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x90A3;&#x8BFB;&#x6587;&#x4EF6;&#x7684;&#x65F6;&#x5019;&#x5C31;&#x6307;&#x5B9A;&#x7F16;&#x7801;&#x6216;&#x8005;&#x8C03;&#x7528; data.toString() &#x65B9;&#x6CD5;&#x8F6C;&#x4E3A;&#x5B57;&#x7B26;&#x3002;</p>
<ul>
<li>&#x8FD9;&#x91CC;&#x56E0;&#x4E3A;&#x4E0D;&#x5904;&#x7406;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x6240;&#x4EE5;&#x5C31;&#x4E0D;&#x8F6C;&#x5B57;&#x7B26;</li>
<li><code>res.end()</code> &#x53EA;&#x80FD;&#x63A5;&#x6536; &#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x636E;&#x6216;&#x8005; &#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5176;&#x5B83;&#x90FD;&#x62A5;&#x9519;</li>
<li>&#x5982;&#x679C;&#x4F20;&#x9012;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5219;&#x53D1;&#x9001;&#x54CD;&#x5E94;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8FD8;&#x4F1A;&#x81EA;&#x52A8;&#x5C06;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x4E3A;&#x4E8C;&#x8FDB;&#x5236;&#x518D;&#x53D1;&#x9001;</li>
<li>&#x5982;&#x679C;&#x76F4;&#x63A5;&#x5C31;&#x4F20;&#x9012;&#x7684;&#x662F;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x636E;&#xFF0C;&#x5219;&#x76F4;&#x63A5;&#x53D1;&#x9001;</li>
</ul>
<blockquote>
<p>&#x5F53;&#x5BA2;&#x6237;&#x7AEF;&#x6D4F;&#x89C8;&#x5668;&#x6536;&#x5230;&#x53D1;&#x9001;&#x7684;&#x54CD;&#x5E94;&#x6570;&#x636E;&#x7684;&#x65F6;&#xFF1A;</p>
</blockquote>
<ul>
<li>&#x6D4F;&#x89C8;&#x5668;&#x4F1A;&#x5148;&#x67E5;&#x770B;&#x54CD;&#x5E94;&#x62A5;&#x6587;&#x5934;&#x4E2D;&#x7684; <code>Content-Type</code> &#x4E2D;&#x7684; <code>charset</code> &#x7F16;&#x7801;&#xFF0C;&#x7136;&#x540E;&#x6839;&#x636E;&#x8BE5;&#x7F16;&#x7801;&#x89E3;&#x6790;&#x6570;&#x636E;</li>
<li>&#x5982;&#x679C;&#x54CD;&#x5E94;&#x62A5;&#x6587;&#x5934;&#x4E2D;&#x6CA1;&#x6709; <code>Content-Type</code> &#x90A3;&#x4E48;&#x6D4F;&#x89C8;&#x5668;&#x5219;&#x6839;&#x636E; HTML &#x7ED3;&#x6784;&#x4E2D;&#x7684; <code>&lt;meta charset=&quot;UTF-8&quot;&gt;</code> &#x6765;&#x89E3;&#x6790;&#x6570;&#x636E;</li>
<li>&#x53EF;&#x4EE5;&#x901A;&#x8FC7; <code>res.writeHead</code> &#x65B9;&#x6CD5;&#x5728;&#x7ED3;&#x675F;&#x54CD;&#x5E94;&#x4E4B;&#x524D;&#xFF0C;&#x5199;&#x54CD;&#x5E94;&#x5934;</li>
<li>&#x67E5;&#x8BE2;&#x7F51;&#x5740;&#xFF1A;<a href="http://tool.oschina.net/commons" target="_blank" rel="external">http://tool.oschina.net/commons</a></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">&apos;http&apos;</span>);</div><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line"><span class="keyword">const</span> server = http.createServer();</div><div class="line">server.on(<span class="string">&apos;request&apos;</span>, (req, res) =&gt; {</div><div class="line">  <span class="keyword">const</span> url = req.url;   </div><div class="line">  <span class="keyword">if</span> (url === <span class="string">&apos;/&apos;</span>) {</div><div class="line">    fs.readFile(<span class="string">&apos;.data/index.html&apos;</span>, (err, data) =&gt; {</div><div class="line">      <span class="keyword">if</span> (err) {</div><div class="line">        <span class="keyword">throw</span> err;</div><div class="line">      }</div><div class="line">    });</div><div class="line">    <span class="comment">// &#x5728;&#x7ED3;&#x675F;&#x54CD;&#x5E94;&#x4E4B;&#x524D;&#xFF0C;&#x5199;&#x54CD;&#x5E94;&#x5934;&#xFF0C;&#x6307;&#x5B9A;charset &#x7F16;&#x7801;</span></div><div class="line">    res.writeHead(<span class="number">202</span>, <span class="string">&apos;OK&apos;</span>, {</div><div class="line">      <span class="string">&apos;Content-Type&apos;</span>: <span class="string">&apos;text/html; charset=utf-8&apos;</span></div><div class="line">    });</div><div class="line">    res.end(data);</div><div class="line">  } <span class="keyword">else</span> <span class="keyword">if</span> (url === <span class="string">&apos;/add&apos;</span>) {</div><div class="line">    fs.readFile(<span class="string">&apos;.data/add.html&apos;</span>, (err, data) =&gt; {</div><div class="line">      <span class="keyword">if</span> (err) {</div><div class="line">        <span class="keyword">throw</span> err;</div><div class="line">      }</div><div class="line">    });</div><div class="line">    res.writeHead(<span class="number">202</span>, <span class="string">&apos;OK&apos;</span>, {</div><div class="line">      <span class="string">&apos;Content-Type&apos;</span>: <span class="string">&apos;text/html; charset=utf-8&apos;</span></div><div class="line">    });</div><div class="line">    res.end(data);</div><div class="line">  } <span class="keyword">else</span> <span class="keyword">if</span> (url === <span class="string">&apos;/about&apos;</span>) {</div><div class="line">    res.writeHead(<span class="number">200</span>, <span class="string">&apos;OK&apos;</span>, {</div><div class="line">      <span class="string">&apos;Content-Type&apos;</span>: <span class="string">&apos;text/plain; charset=utf-8&apos;</span></div><div class="line">    })</div><div class="line">    res.end(<span class="string">&apos;about page&apos;</span>);</div><div class="line">  } <span class="keyword">else</span> {</div><div class="line">    fs.readFile(<span class="string">&apos;.data/404.html&apos;</span>, (err, data) =&gt; {</div><div class="line">      <span class="keyword">if</span> (err) {</div><div class="line">        <span class="keyword">throw</span> err;</div><div class="line">      }</div><div class="line">    });</div><div class="line">    res.writeHead(<span class="number">404</span>, <span class="string">&apos;Not Found&apos;</span>, {</div><div class="line">      <span class="string">&apos;Content-Type&apos;</span>: <span class="string">&apos;text/html; charset=utf-8&apos;</span></div><div class="line">    })</div><div class="line">    res.end(data);</div><div class="line">  }</div><div class="line">  </div><div class="line">});</div><div class="line">server.listen(<span class="number">3000</span>, () =&gt; {</div><div class="line">  <span class="built_in">console</span>.log(<span class="string">&apos;Server is running at port 3000...&apos;</span>);</div><div class="line">});</div></pre></td></tr></table></figure>
<h2 id="&#x5904;&#x7406;&#x9875;&#x9762;&#x4E2D;&#x7684;&#x52A8;&#x6001;&#x8D44;&#x6E90;&#x548C;&#x9759;&#x6001;&#x8D44;&#x6E90;"><a href="#&#x5904;&#x7406;&#x9875;&#x9762;&#x4E2D;&#x7684;&#x52A8;&#x6001;&#x8D44;&#x6E90;&#x548C;&#x9759;&#x6001;&#x8D44;&#x6E90;" class="headerlink" title="&#x5904;&#x7406;&#x9875;&#x9762;&#x4E2D;&#x7684;&#x52A8;&#x6001;&#x8D44;&#x6E90;&#x548C;&#x9759;&#x6001;&#x8D44;&#x6E90;"></a>&#x5904;&#x7406;&#x9875;&#x9762;&#x4E2D;&#x7684;&#x52A8;&#x6001;&#x8D44;&#x6E90;&#x548C;&#x9759;&#x6001;&#x8D44;&#x6E90;</h2><p>&#x540C;&#x4E00;&#x4E2A;&#x9875;&#x9762;&#x4E2D;&#x6709;&#x591A;&#x4E2A;&#x5916;&#x94FE;&#xFF0C;&#x4E0D;&#x662F;&#x6307; a &#x6807;&#x7B7E;&#x3002;</p>
<p>&#x5F53;&#x6D4F;&#x89C8;&#x5668;&#x83B7;&#x53D6;&#x5230;&#x5F53;&#x524D;&#x54CD;&#x5E94;&#x7684; HTML &#x683C;&#x5F0F;&#x5B57;&#x7B26;&#x4E32;&#x4E4B;&#x540E;&#xFF0C;&#x6D4F;&#x89C8;&#x5668;&#x4ECE;&#x4E0A;&#x5230;&#x4E0B;&#x4F9D;&#x6B21;&#x89E3;&#x6790;&#x5B57;&#x7B26;&#x4E32;&#xFF08;HTML &#x7ED3;&#x6784;&#x6587;&#x6863;&#xFF09;&#x3002;</p>
<p>&#x5728;&#x89E3;&#x6790;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x53D1;&#x73B0;&#x6709; link img script iframe &#x7B49;&#x5177;&#x6709;  src &#x6216; href &#x7684;&#x6807;&#x7B7E;&#xFF1A;a &#x6807;&#x7B7E;&#x548C;&#x4ED6;&#x4EEC;&#x4E0D;&#x4E00;&#x6837;&#xFF0C;a &#x6807;&#x7B7E;&#x662F;&#x7528;&#x6765;&#x8DF3;&#x8F6C;&#x7684;&#xFF0C;&#x8D44;&#x6E90;&#x5728;&#x53E6;&#x4E00;&#x4E2A;&#x9875;&#x9762;&#x3002;&#x5219;&#xFF0C;&#x6D4F;&#x89C8;&#x5668;&#x4E3B;&#x52A8;&#x5BF9;&#x8BE5;&#x8D44;&#x6E90;&#x6307;&#x5411;&#x7684;&#x5730;&#x5740;&#x53D1;&#x8D77;&#x8BF7;&#x6C42;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">&apos;http&apos;</span>);</div><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&apos;path&apos;</span>);</div><div class="line">http</div><div class="line">  .createServer()</div><div class="line">  .on(<span class="string">&apos;request&apos;</span>, (req, res) =&gt; {</div><div class="line">    <span class="keyword">const</span> url = req.url;</div><div class="line">    <span class="keyword">if</span> (url === <span class="string">&apos;/&apos;</span>) {</div><div class="line">      fs.readFile(<span class="string">&apos;./data/static/index.html&apos;</span>, (err, data) =&gt; {</div><div class="line">        <span class="keyword">if</span> (err) {</div><div class="line">          <span class="keyword">throw</span> err;</div><div class="line">        }</div><div class="line">        res.writeHead(<span class="number">200</span>, {</div><div class="line">          <span class="string">&apos;Content-Type&apos;</span>: <span class="string">&apos;text/html; charset=utf-8&apos;</span></div><div class="line">        });</div><div class="line">        res.end(data);</div><div class="line">      });</div><div class="line">    } <span class="keyword">else</span> <span class="keyword">if</span> (url === <span class="string">&apos;/css/main.css&apos;</span>) {</div><div class="line">      fs.readFile(<span class="string">&apos;./data/static/css/main.css&apos;</span>, (err, data) =&gt; {</div><div class="line">        <span class="keyword">if</span> (err) {</div><div class="line">          <span class="keyword">throw</span> err;</div><div class="line">        }</div><div class="line">        res.writeHead(<span class="number">200</span>, {</div><div class="line">          <span class="string">&apos;Content-Type&apos;</span>: <span class="string">&apos;text/css; charset=utf-8&apos;</span></div><div class="line">        });</div><div class="line">        res.end(data);</div><div class="line">      });</div><div class="line">    } <span class="keyword">else</span> <span class="keyword">if</span> (url === <span class="string">&apos;/img/96102-106.jpg&apos;</span>) {</div><div class="line">      fs.readFile(<span class="string">&apos;./data/static/img/96102-106.jpg&apos;</span>, (err, data) =&gt; {</div><div class="line">        <span class="keyword">if</span> (err) {</div><div class="line">          <span class="keyword">throw</span> err;</div><div class="line">        }</div><div class="line">        res.writeHead(<span class="number">200</span>, {</div><div class="line">          <span class="string">&apos;Content-Type&apos;</span>: <span class="string">&apos;image/jpeg&apos;</span></div><div class="line">        });</div><div class="line">        res.end(data);</div><div class="line">      });</div><div class="line">    } <span class="keyword">else</span> <span class="keyword">if</span> (url === <span class="string">&apos;/js/main.js&apos;</span>) {</div><div class="line">      fs.readFile(<span class="string">&apos;./data/static/js/main.js&apos;</span>, (err, data) =&gt; {</div><div class="line">        <span class="keyword">if</span> (err) {</div><div class="line">          <span class="keyword">throw</span> err;</div><div class="line">        }</div><div class="line">        res.writeHead(<span class="number">200</span>, {</div><div class="line">          <span class="string">&apos;Content-Type&apos;</span>: <span class="string">&apos;application/x-javascript; charset=utf-8&apos;</span></div><div class="line">        });</div><div class="line">        res.end(data);</div><div class="line">      });</div><div class="line">    }</div><div class="line">  })</div><div class="line">  .listen(<span class="number">3000</span>, () =&gt; {</div><div class="line">    <span class="built_in">console</span>.log(<span class="string">&apos;Server is running at port 3000.&apos;</span>);</div><div class="line">  });</div></pre></td></tr></table></figure>
<p>index.html &#x6587;&#x6863;&#x5185;&#x5BB9;</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- src: ./data/static/index.html --&gt;</span></div><div class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></div><div class="line">  <span class="comment">&lt;!-- http://127.0.0.1:3000/css/main.css --&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet&quot;</span> <span class="attr">href</span>=<span class="string">&quot;css/main.css&quot;</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet&quot;</span> <span class="attr">href</span>=<span class="string">&quot;css/a.css&quot;</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">h1</span>&gt;</span>&#x9996;&#x9875;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line">  <span class="comment">&lt;!-- http://127.0.0.1:3000/img/96102-106.jpg --&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">&quot;img/96102-106.jpg&quot;</span> <span class="attr">alt</span>=<span class="string">&quot;&quot;</span>&gt;</span></div><div class="line">  <span class="comment">&lt;!-- http://127.0.0.1:3000/js/main.js --&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;js/main.js&quot;</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></div></pre></td></tr></table></figure>
<h2 id="&#x89E3;&#x51B3;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x7684;-mime-&#x7C7B;&#x578B;"><a href="#&#x89E3;&#x51B3;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x7684;-mime-&#x7C7B;&#x578B;" class="headerlink" title="&#x89E3;&#x51B3;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x7684; mime &#x7C7B;&#x578B;"></a>&#x89E3;&#x51B3;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x7684; mime &#x7C7B;&#x578B;</h2><ul>
<li>&#x2018;Content-Type&#x2019;: mime.lookup(url);</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">&apos;http&apos;</span>);</div><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&apos;path&apos;</span>);</div><div class="line"><span class="keyword">const</span> mime = <span class="built_in">require</span>(<span class="string">&apos;mime&apos;</span>);</div><div class="line">http</div><div class="line">  .createServer()</div><div class="line">  .on(<span class="string">&apos;request&apos;</span>, (req, res) =&gt; {</div><div class="line">    <span class="comment">// /  index.html</span></div><div class="line">    <span class="comment">// /add add.html</span></div><div class="line">    <span class="comment">// /404 404.html</span></div><div class="line">    <span class="comment">// http://127.0.0.1:3000/public/css/main.css</span></div><div class="line">    <span class="comment">// http://127.0.0.1:3000/public/js/main.js</span></div><div class="line">    <span class="keyword">const</span> url = req.url</div><div class="line">    <span class="keyword">if</span> (url === <span class="string">&apos;/&apos;</span>) {</div><div class="line">      fs.readFile(<span class="string">&apos;./views/index.html&apos;</span>, (err, data) =&gt; {</div><div class="line">        <span class="keyword">if</span> (err) {</div><div class="line">          <span class="keyword">throw</span> err;</div><div class="line">        }</div><div class="line">        res.writeHead(<span class="number">200</span>, {</div><div class="line">          <span class="string">&apos;Content-Type&apos;</span>: <span class="string">&apos;text/html; charset=utf-8&apos;</span></div><div class="line">        });</div><div class="line">        res.end(data);</div><div class="line">      });</div><div class="line">    } <span class="keyword">else</span> <span class="keyword">if</span> (url.startsWith(<span class="string">&apos;/public/&apos;</span>)) {</div><div class="line">      <span class="comment">// /public/css/main.css</span></div><div class="line">      <span class="comment">// /public/js/main.js</span></div><div class="line">      fs.readFile(<span class="string">`.<span class="subst">${url}</span>`</span>, (err, data) =&gt; {</div><div class="line">        <span class="keyword">if</span> (err) {</div><div class="line">          <span class="keyword">throw</span> err;</div><div class="line">        }</div><div class="line">        <span class="comment">// Content-Type &#xFF1F;</span></div><div class="line">        <span class="comment">// &#x65E0;&#x8BBA;&#x662F;&#x4EFB;&#x4F55;&#x8D44;&#x6E90;&#xFF0C;&#x90FD;&#x6700;&#x597D;&#x8981;&#x6709; Content-Type</span></div><div class="line">        res.writeHead(<span class="number">200</span>, {</div><div class="line">          <span class="string">&apos;Content-Type&apos;</span>: mime.lookup(url);</div><div class="line">        });</div><div class="line">        res.end(data);</div><div class="line">      });</div><div class="line">    } <span class="keyword">else</span> <span class="keyword">if</span> (url === <span class="string">&apos;/add&apos;</span>) {</div><div class="line">      fs.readFile(<span class="string">&apos;./views/add.html&apos;</span>, (err, data) =&gt; {</div><div class="line">        <span class="keyword">if</span> (err) {</div><div class="line">          <span class="keyword">throw</span> err;</div><div class="line">        }</div><div class="line">        res.writeHead(<span class="number">200</span>, {</div><div class="line">          <span class="string">&apos;Content-Type&apos;</span>: <span class="string">&apos;text/html; charset=utf-8&apos;</span></div><div class="line">        });</div><div class="line">        res.end(data);</div><div class="line">      });</div><div class="line">    } <span class="keyword">else</span> {</div><div class="line">      fs.readFile(<span class="string">&apos;./views/404.html&apos;</span>, (err, data) =&gt; {</div><div class="line">        <span class="keyword">if</span> (err) {</div><div class="line">          <span class="keyword">throw</span> err;</div><div class="line">        }</div><div class="line">        res.writeHead(<span class="number">200</span>, {</div><div class="line">          <span class="string">&apos;Content-Type&apos;</span>: <span class="string">&apos;text/html; charset=utf-8&apos;</span></div><div class="line">        });</div><div class="line">        res.end(data);</div><div class="line">      });</div><div class="line">    }</div><div class="line">  })</div><div class="line">  .listen(<span class="number">3000</span>, () =&gt; {</div><div class="line">    <span class="built_in">console</span>.log(<span class="string">&apos;Server is running at port 3000.&apos;</span>);</div><div class="line">  });</div></pre></td></tr></table></figure>
<p>./views/index.html</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet&quot;</span> <span class="attr">href</span>=<span class="string">&quot;public/css/main.css&quot;</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet&quot;</span> <span class="attr">href</span>=<span class="string">&quot;public/css/a.css&quot;</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">h1</span>&gt;</span>&#x9996;&#x9875;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;public/js/main.js&quot;</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></div></pre></td></tr></table></figure>
<h2 id="http-&#x548C;-net-&#x7684;&#x533A;&#x522B;&#x4E0E;&#x8054;&#x7CFB;"><a href="#http-&#x548C;-net-&#x7684;&#x533A;&#x522B;&#x4E0E;&#x8054;&#x7CFB;" class="headerlink" title="http &#x548C; net &#x7684;&#x533A;&#x522B;&#x4E0E;&#x8054;&#x7CFB;"></a>http &#x548C; net &#x7684;&#x533A;&#x522B;&#x4E0E;&#x8054;&#x7CFB;</h2><p>&#x89E3;&#x91CA;&#x6D4F;&#x89C8;&#x5668;&#x548C;&#x670D;&#x52A1;&#x5668;&#x7684;&#x4EA4;&#x4E92;&#x672C;&#x8D28;&#xFF1A;</p>
<ul>
<li>&#x672C;&#x8D28;&#x4E0A;&#x5C31;&#x662F;&#x6D4F;&#x89C8;&#x5668;&#x901A;&#x8FC7; Socket &#x548C; &#x670D;&#x52A1;&#x5668; Socket &#x8FDB;&#x884C;&#x901A;&#x4FE1;</li>
<li>&#x53CC;&#x65B9;&#x90FD;&#x901A;&#x8FC7; HTTP &#x534F;&#x8BAE;&#x8FDB;&#x884C;&#x4EA4;&#x6D41;</li>
<li>net &#x6A21;&#x5757;&#x5C31;&#x662F;&#x4F20;&#x8F93;&#x5C42;&#x7684;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#xFF0C;&#x53EA;&#x662F;&#x4E3A;&#x4E86;&#x7EAF;&#x7CB9;&#x7684;&#x6536;&#x53D1;&#x6570;&#x636E;</li>
<li>http &#x6A21;&#x5757;&#x6784;&#x5EFA;&#x4E0E; net &#x6A21;&#x5757;&#x4E4B;&#x4E0A;&#xFF0C;&#x53EA;&#x4E0D;&#x8FC7;&#x5BF9;&#x4E8E;&#x6536;&#x53D1;&#x7684;&#x6570;&#x636E;&#x4F1A;&#x8FDB;&#x884C;&#x89E3;&#x6790;&#x548C;&#x5305;&#x88C5;</li>
<li>&#x6240;&#x6709;&#x7684;BS&#x6A21;&#x578B;&#x90FD;&#x662F;&#x4F7F;&#x7528;&#x7684; HTTP &#x534F;&#x8BAE;&#x8FDB;&#x884C;&#x6570;&#x636E;&#x7684;&#x89E3;&#x6790;&#x548C;&#x5305;&#x88C5;</li>
</ul>
<p>&#x4E00;&#x4E0B;&#x4EE3;&#x7801;&#x4EC5;&#x4EC5;&#x7528;&#x6765;&#x8BF4;&#x660E; net &#x548C; HTTP &#x4E4B;&#x95F4;&#x7684;&#x8054;&#x7CFB;&#x4E0E;&#x533A;&#x522B;&#xFF0C;&#x6CA1;&#x6709;&#x5B9E;&#x9645;&#x5E94;&#x7528;&#x610F;&#x4E49;&#x3002;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> net = <span class="built_in">require</span>(<span class="string">&apos;net&apos;</span>);</div><div class="line">net</div><div class="line">  .createServer();</div><div class="line">  .on(<span class="string">&apos;connection&apos;</span>, socket =&gt; {</div><div class="line">    socket.on(<span class="string">&apos;data&apos;</span>, data =&gt; {</div><div class="line">      <span class="comment">// console.log(data.toString())</span></div><div class="line">      <span class="comment">// &#x81EA;&#x5DF1;&#x52A8;&#x624B;&#x89E3;&#x6790;&#x8BF7;&#x6C42;&#x62A5;&#x6587;</span></div><div class="line">      <span class="comment">// 1. &#x6309;&#x7167;&#x7A7A;&#x884C;&#x5C06;&#x8BF7;&#x6C42;&#x4F53;&#x548C;&#x8BF7;&#x6C42;&#x62A5;&#x6587;&#x5206;&#x5272;</span></div><div class="line">      <span class="comment">// 2. &#x6309;&#x7167;&#x6362;&#x884C;&#x5C06;&#x8BF7;&#x6C42;&#x5934;&#x5206;&#x5272;</span></div><div class="line">      <span class="comment">//    &#x6570;&#x7EC4;&#x4E2D;&#x7B2C; 0 &#x9879;&#x5C31;&#x662F;&#x8BF7;&#x6C42;&#x9996;&#x884C;</span></div><div class="line">      <span class="comment">//    &#x5176;&#x5B83;&#x6240;&#x6709;&#x9879;&#x90FD;&#x662F;&#x8BF7;&#x6C42;&#x9996;&#x90E8;&#x5B57;&#x6BB5;</span></div><div class="line">      <span class="built_in">console</span>.log(socket.remoteAddress, socket.remotePort);</div><div class="line">      data = data.toString();</div><div class="line">      <span class="keyword">const</span> requestContext = data.split(<span class="string">&apos;\r\n\r\n&apos;</span>);</div><div class="line">      <span class="keyword">const</span> requestHead = requestContext[<span class="number">0</span>];</div><div class="line">      <span class="keyword">const</span> requestBody = requestContext[<span class="number">1</span>];</div><div class="line">      <span class="keyword">const</span> req = {};</div><div class="line">      <span class="comment">// url method httpVersion</span></div><div class="line">      <span class="comment">// headers</span></div><div class="line">      <span class="comment">// { key: value, key:value } &#x7ED9;&#x5728;&#x7ED9; req &#x5BF9;&#x8C61;&#x7684; headers</span></div><div class="line">      <span class="keyword">const</span> requestHeadFirst = requestHead.split(<span class="string">&apos;\r\n&apos;</span>)[<span class="number">0</span>].split(<span class="string">&apos; &apos;</span>);</div><div class="line">      req.method = requestHeadFirst[<span class="number">0</span>];</div><div class="line">      req.url = requestHeadFirst[<span class="number">1</span>];</div><div class="line">      req.httpVersion = requestHeadFirst[<span class="number">2</span>];</div><div class="line">      req.headers = {};</div><div class="line">      <span class="keyword">const</span> requestHeadFileds = requestHead.split(<span class="string">&apos;\r\n&apos;</span>).slice(<span class="number">1</span>);</div><div class="line">      <span class="keyword">for</span> (<span class="keyword">let</span> item <span class="keyword">of</span> requestHeadFileds) {</div><div class="line">        <span class="keyword">const</span> tmp = item.split(<span class="string">&apos;: &apos;</span>);</div><div class="line">        req.headers[tmp[<span class="number">0</span>]] = tmp[<span class="number">1</span>];</div><div class="line">      }</div><div class="line">      <span class="comment">// net &#x6A21;&#x5757;&#x4E2D;&#x53D1;&#x9001;&#x6570;&#x636E;&#x53EA;&#x662F;&#x7EAF;&#x7CB9;&#x7684;&#x53D1;&#x9001;&#x4F60;&#x4F20;&#x5165;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x6709;&#x4E00;&#x884C;&#x7A7A;&#x767D;&#x884C;&#x3002;</span></div><div class="line">      socket.write(<span class="string">`</span></div><div class="line">HTTP/1.1 200 OK</div><div class="line">Server: Itcast</div><div class="line">Connection: keep-alive</div><div class="line">foo: bar</div><div class="line">Content-Type: text/html; charset=utf-8</div><div class="line"></div><div class="line">&lt;h1&gt;hello world&lt;/h1&gt;`);</div><div class="line">      socket.end();</div><div class="line">    });</div><div class="line">  })</div><div class="line">  .listen(<span class="number">3000</span>, () =&gt; {</div><div class="line">    <span class="built_in">console</span>.log(<span class="string">&apos;server is running at port 3000.&apos;</span>)</div><div class="line">  });</div></pre></td></tr></table></figure>
<p>&#x5F53;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x5730;&#x5740;&#x680F;&#x8F93;&#x5165;&#x4E86;&#x4E00;&#x4E2A;&#x5730;&#x5740;&#xFF1A;<a href="http://127.0.0.1:3000/" target="_blank" rel="external">http://127.0.0.1:3000/</a></p>
<p>&#x6D4F;&#x89C8;&#x5668;&#x6309;&#x7167; HTTP &#x534F;&#x8BAE;&#x5C06;&#x4F60;&#x8F93;&#x5165;&#x7684;&#x5730;&#x5740;&#x5305;&#x88C5;&#x6210; HTTP &#x8BF7;&#x6C42;&#x62A5;&#x6587;&#xFF0C;&#x5185;&#x5BB9;&#x5982;&#x4E0B;&#xFF1A;</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">GET / HTTP/1.1</div><div class="line">Host: 127.0.0.1:3000</div><div class="line">Connection: keep-alive</div><div class="line">Cache-Control: max-age=0</div><div class="line">Upgrade-Insecure-Requests: 1</div><div class="line">User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36</div><div class="line">Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8</div><div class="line">Accept-Encoding: gzip, deflate, sdch</div><div class="line">Accept-Language: zh-CN,zh;q=0.8</div></pre></td></tr></table></figure>
<p>&#x8BF7;&#x6C42;&#x62A5;&#x6587;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<ul>
<li>&#x8BF7;&#x6C42;&#x5934;<ul>
<li>&#x8BF7;&#x6C42;&#x9996;&#x884C;<ul>
<li>&#x8BF7;&#x6C42;&#x65B9;&#x6CD5; &#x8BF7;&#x6C42;&#x8DEF;&#x5F84; HTTP&#x534F;&#x8BAE;&#x7248;&#x672C;</li>
</ul>
</li>
<li>&#x8BF7;&#x6C42;&#x9996;&#x90E8;&#x5B57;&#x6BB5;<ul>
<li>&#x9996;&#x90FD;&#x5B57;&#x6BB5;&#x4E2D;&#x653E;&#x4E00;&#x4E9B;&#x989D;&#x5916;&#x7684;&#x9644;&#x52A0;&#x4FE1;&#x606F;</li>
<li>&#x4F8B;&#x5982; User-Agent &#x8868;&#x793A;&#x544A;&#x8BC9;&#x670D;&#x52A1;&#x5668;&#x6211;&#x8FD9;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;&#x662F;&#x4EC0;&#x4E48;<ul>
<li>&#x8FD9;&#x91CC;&#x4E3A;&#x4EC0;&#x4E48;&#x6709;&#x5404;&#x79CD;&#x6D4F;&#x89C8;&#x5668;&#x7684;&#x6807;&#x8BC6;</li>
<li>&#x539F;&#x56E0;&#x5728;&#x65E9;&#x671F;&#x7684;&#x7F51;&#x9875;&#x6709;&#x5404;&#x79CD;&#x5404;&#x6837;&#x517C;&#x5BB9;&#x6027;&#x95EE;&#x9898;</li>
<li>&#x8FD9;&#x4E2A;&#x5B57;&#x6BB5;&#x8FD8;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x7EDF;&#x8BA1;&#x6D4F;&#x89C8;&#x5668;&#x7684;&#x4F7F;&#x7528;&#x91CF;&#x5360;&#x6BD4;&#x60C5;&#x51B5;</li>
</ul>
</li>
<li>Accept<ul>
<li>&#x65E9;&#x671F;&#x7684; HTTP 0.9 &#x4E2D;&#xFF0C;&#x53EA;&#x80FD;&#x6536;&#x53D1;&#x666E;&#x901A;&#x5B57;&#x7B26;&#x6570;&#x636E; &#x4E0D;&#x652F;&#x6301;&#x56FE;&#x7247;&#x7B49;&#x5BCC;&#x6587;&#x672C;&#x4FE1;&#x606F;</li>
<li>&#x5386;&#x53F2;&#x539F;&#x56E0;&#xFF0C;&#x73B0;&#x4EE3;&#x7684;&#x670D;&#x52A1;&#x5668;&#x548C;&#x5BA2;&#x6237;&#x7AEF;&#x6D4F;&#x89C8;&#x5668;&#x5DF2;&#x7ECF;&#x4E0D;&#x9700;&#x8981;&#x8FD9;&#x4E2A;&#x4E1C;&#x897F;</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>&#x7A7A;&#x884C;</li>
<li>&#x8BF7;&#x6C42;&#x4F53;<ul>
<li>&#x5982;&#x679C;&#x662F; post &#x8BF7;&#x6C42;&#x624D;&#x6709;&#x8BF7;&#x6C42;&#x4F53;</li>
<li>&#x5982;&#x679C;&#x6709;&#x8BF7;&#x6C42;&#x4F53;&#xFF0C;&#x5219;&#x8BF7;&#x6C42;&#x4F53;&#x662F;&#x5728;&#x8BF7;&#x6C42;&#x5934;&#x7684;&#x56DE;&#x8F66;&#x6362;&#x884C;&#x4E4B;&#x540E;</li>
<li>&#x5982;&#x679C;&#x6CA1;&#x6709;&#xFF0C;&#x4E5F;&#x4F1A;&#x6709;&#x4E00;&#x4E2A;&#x7A7A;&#x884C;&#x5B58;&#x5728;</li>
</ul>
</li>
</ul>
<p>&#x54CD;&#x5E94;&#x62A5;&#x6587;&#xFF1A;</p>
<ul>
<li>&#x54CD;&#x5E94;&#x5934;<ul>
<li>&#x54CD;&#x5E94;&#x9996;&#x884C;<ul>
<li>HTTP&#x534F;&#x8BAE;&#x7248;&#x672C; &#x72B6;&#x6001;&#x7801; &#x72B6;&#x6001;&#x77ED;&#x8BED;</li>
</ul>
</li>
<li>&#x54CD;&#x5E94;&#x9996;&#x90E8;&#x5B57;&#x6BB5;</li>
</ul>
</li>
<li>&#x7A7A;&#x884C;</li>
<li>&#x54CD;&#x5E94;&#x4F53;<ul>
<li>&#x6240;&#x6709;&#x7684;&#x54CD;&#x5E94;&#x6570;&#x636E;&#x90FD;&#x5728;&#x54CD;&#x5E94;&#x5934;&#x4E4B;&#x540E;&#x7684;&#x7A7A;&#x884C;&#x4E4B;&#x540E;</li>
</ul>
</li>
</ul>
<h2 id="net-&#x548C;-http-&#x6A21;&#x5757;&#x7684;&#x5173;&#x7CFB;&#xFF1A;"><a href="#net-&#x548C;-http-&#x6A21;&#x5757;&#x7684;&#x5173;&#x7CFB;&#xFF1A;" class="headerlink" title="net &#x548C; http &#x6A21;&#x5757;&#x7684;&#x5173;&#x7CFB;&#xFF1A;"></a>net &#x548C; http &#x6A21;&#x5757;&#x7684;&#x5173;&#x7CFB;&#xFF1A;</h2><ul>
<li>http &#x6A21;&#x5757;&#x662F;&#x6784;&#x5EFA;&#x4E0E; net &#x6A21;&#x5757;&#x4E4B;&#x4E0A;&#x7684;</li>
<li>http &#x4E2D;&#x7684;&#x6536;&#x53D1;&#x6570;&#x636E;&#x8FD8;&#x662F;&#x901A;&#x8FC7; net &#x6A21;&#x5757;&#x4E2D;&#x7684; Socket &#x6536;&#x53D1;&#x6570;&#x636E;&#x7684;</li>
<li>http &#x4F1A;&#x5C06;&#x6536;&#x53D1;&#x7684;&#x6570;&#x636E;&#x6309;&#x7167; HTTP &#x534F;&#x8BAE;&#x81EA;&#x52A8;&#x5E2E;&#x4F60;&#x89E3;&#x6790;&#x548C;&#x5305;&#x88C5;<ul>
<li>&#x4F8B;&#x5982; http &#x6A21;&#x5757;&#x81EA;&#x52A8;&#x5C06;&#x8BF7;&#x6C42;&#x62A5;&#x6587;&#x89E3;&#x6790;&#x51FA;&#x6765;&#xFF0C;&#x7136;&#x540E;&#x6302;&#x8F7D;&#x7ED9;&#x4E86; req &#x8BF7;&#x6C42;&#x5BF9;&#x8C61;</li>
<li>&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; req &#x8BF7;&#x6C42;&#x5BF9;&#x8C61;&#x53BB;&#x62FF;&#x5230;&#x4F60;&#x60F3;&#x8981;&#x7684;&#x4FE1;&#x606F;</li>
</ul>
</li>
<li>&#x4E3A;&#x4EC0;&#x4E48;&#x65E2;&#x6709; net &#x53C8;&#x6709; http &#x5462;&#xFF1F;<ul>
<li>http &#x53EA;&#x662F;&#x4E00;&#x4E2A;&#x57FA;&#x4E8E; net &#x4E4B;&#x4E0A;&#x7684;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#xFF0C;&#x8BE5;&#x6A21;&#x5757;&#x9075;&#x5FAA;&#x7684; http &#x534F;&#x8BAE;</li>
<li>&#x4F1A;&#x5BF9;&#x6536;&#x53D1;&#x7684;&#x6570;&#x636E;&#x8FDB;&#x884C; &#x534F;&#x8BAE;&#x683C;&#x5F0F;&#x89E3;&#x6790;&#x548C;&#x5305;&#x88C5;</li>
<li>HTTP &#x534F;&#x8BAE;&#x53EA;&#x662F;&#x9002;&#x7528;&#x4E8E;B/S&#x6A21;&#x578B;</li>
</ul>
</li>
<li>&#x6709;&#x7684;&#x4E1A;&#x52A1;&#x529F;&#x80FD;&#x4F7F;&#x7528;&#x7684;&#x662F;&#x522B;&#x7684;&#x534F;&#x8BAE;<ul>
<li>&#x4F8B;&#x5982; &#x4E00;&#x4E9B;&#x667A;&#x80FD;&#x7EC8;&#x7AEF;&#xFF0C;&#x5C31;&#x7528;&#x7684;&#x662F;&#x522B;&#x7684;&#x534F;&#x8BAE;&#xFF0C;&#x800C;&#x4E0D;&#x662F; HTTP</li>
<li>&#x4F46;&#x662F;&#x4ED6;&#x4EEC;&#x90FD;&#x662F;&#x57FA;&#x4E8E;&#x6700;&#x57FA;&#x672C;&#x7684; Socket &#x7F51;&#x7EDC;&#x7F16;&#x7A0B;&#x6A21;&#x578B;&#x800C;&#x6784;&#x5EFA;&#x7684;</li>
</ul>
</li>
</ul>
<h2 id="&#x6D4F;&#x89C8;&#x5668;&#x7684;&#x672C;&#x8D28;"><a href="#&#x6D4F;&#x89C8;&#x5668;&#x7684;&#x672C;&#x8D28;" class="headerlink" title="&#x6D4F;&#x89C8;&#x5668;&#x7684;&#x672C;&#x8D28;"></a>&#x6D4F;&#x89C8;&#x5668;&#x7684;&#x672C;&#x8D28;</h2><ul>
<li>Socket &#x5BA2;&#x6237;&#x7AEF;<ul>
<li>&#x6536;&#x53D1;&#x6570;&#x636E;</li>
</ul>
</li>
<li>&#x6E32;&#x67D3; HTML&#x3001;CSS</li>
<li>&#x89E3;&#x6790;&#x548C;&#x6267;&#x884C; JavaScript &#x4EE3;&#x7801;</li>
</ul>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div>感谢您的支持！</div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>打 赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="http://ofy7k3v4s.bkt.clouddn.com/wcpayCode.jpg" alt="Hongqin Ma WeChat Pay"/>
          <p>微信打赏</p>
        </div>
      
      
        <div id="alipay" style="display: inline-block">
          <img id="alipay_qr" src="http://ofy7k3v4s.bkt.clouddn.com/apayCode.jpg" alt="Hongqin Ma Alipay"/>
          <p>支付宝打赏</p>
        </div>
      
    </div>
  </div>


      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Node-js/" rel="tag">#Node.js</a>
          
            <a href="/tags/http/" rel="tag">#http</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/10/02/s006-node-网络编程-终端聊天室/" rel="next" title="Node-网络编程-终端聊天室">
                <i class="fa fa-chevron-left"></i> Node-网络编程-终端聊天室
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/10/14/s008-node-相册小项目（上）/" rel="prev" title="Node-相册小项目（上）">
                Node-相册小项目（上） <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <div class="ds-share flat" data-thread-key="2016/10/09/s007-node-http-网站/"
     data-title="Node-http-网站"
     data-content=""
     data-url="https://hongqinma.github.io/2016/10/09/s007-node-http-网站/">
  <div class="ds-share-inline">
    <ul  class="ds-share-icons-16">

      <li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
      <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
      <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
      <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
      <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>

    </ul>
    <div class="ds-share-icons-more">
    </div>
  </div>
</div>
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2016/10/09/s007-node-http-网站/"
           data-title="Node-http-网站" data-url="https://hongqinma.github.io/2016/10/09/s007-node-http-网站/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            Overview
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="//schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/photo.jpg"
               alt="Hongqin Ma" />
          <p class="site-author-name" itemprop="name">Hongqin Ma</p>
          <p class="site-description motion-element" itemprop="description">Spell at all costs， to rush you in the future. </p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">68</span>
              <span class="site-state-item-name">posts</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">16</span>
                <span class="site-state-item-name">categories</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">57</span>
                <span class="site-state-item-name">tags</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/HongqinMa" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://twitter.com/HongqinMa" target="_blank" title="Twitter">
                  
                    <i class="fa fa-fw fa-twitter"></i>
                  
                  Twitter
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.facebook.com/profile.php?id=100006581058735" target="_blank" title="Facebook">
                  
                    <i class="fa fa-fw fa-facebook"></i>
                  
                  Facebook
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://www.zhihu.com/people/HirakuMa" target="_blank" title="知乎">
                  
                    <i class="fa fa-fw fa-share-alt"></i>
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              Links
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/rubylouvre/" title="司徒正美" target="_blank">司徒正美</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/sanshi/" title="三生石上" target="_blank">三生石上</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/TomXu/" title="汤姆大叔" target="_blank">汤姆大叔</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://gold.xitu.io/" title="掘金" target="_blank">掘金</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://segmentfault.com/" title="segmentfault" target="_blank">segmentfault</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.jianshu.com/users/1cbb94e5ed4d/latest_articles" title="简书" target="_blank">简书</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://home.cnblogs.com/" title="博客园" target="_blank">博客园</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://www.awesomes.cn/" title="Awesomes" target="_blank">Awesomes</a>
                </li>
              
            </ul>
          </div>
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#http-模块的简单使用"><span class="nav-number">1.</span> <span class="nav-text">http 模块的简单使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#对不同的请求发送不同的响应"><span class="nav-number">2.</span> <span class="nav-text">对不同的请求发送不同的响应</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#根据不同请求响应不同内容"><span class="nav-number">3.</span> <span class="nav-text">根据不同请求响应不同内容</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#处理页面中的静态资源"><span class="nav-number">4.</span> <span class="nav-text">处理页面中的静态资源</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#处理页面中的动态资源和静态资源"><span class="nav-number">5.</span> <span class="nav-text">处理页面中的动态资源和静态资源</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#解决静态资源的-mime-类型"><span class="nav-number">6.</span> <span class="nav-text">解决静态资源的 mime 类型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#http-和-net-的区别与联系"><span class="nav-number">7.</span> <span class="nav-text">http 和 net 的区别与联系</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#net-和-http-模块的关系："><span class="nav-number">8.</span> <span class="nav-text">net 和 http 模块的关系：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#浏览器的本质"><span class="nav-number">9.</span> <span class="nav-text">浏览器的本质</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Hongqin Ma</span>
</div>

<div class="powered-by">
  Powered by <a class="theme-link" href="https://hexo.io">Hexo</a>
</div>

<div class="theme-info">
  Theme -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<span id="busuanzi_container_site_pv">
    &nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;Page Views: <span id="busuanzi_value_site_pv"></span> 
</span>

        

<div class="busuanzi-count">

  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv"><i class="fa fa-user"></i><span class="busuanzi-value" id="busuanzi_value_site_uv"></span></span>
  

  
    <span class="site-pv"><i class="fa fa-eye"></i><span class="busuanzi-value" id="busuanzi_value_site_pv"></span></span>
  
  
</div>



        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.2"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.2"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"Hiraku"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  

  

  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("PPmoIQxeu8Sd21h6Tl9TthNx-gzGzoHsz", "rTvbY2H7DvWzRipiBwn72RQ4");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  
  <!-- custom analytics part create by xiamo -->
<script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
<script>AV.initialize("PPmoIQxeu8Sd21h6Tl9TthNx-gzGzoHsz", "rTvbY2H7DvWzRipiBwn72RQ4");</script>
<script>
function showTime(Counter) {
  var query = new AV.Query(Counter);
  $(".leancloud_visitors").each(function() {
    var url = $(this).attr("id").trim();
    query.equalTo("url", url);
    query.find({
      success: function(results) {
        if (results.length == 0) {
          var content = '0 ' + $(document.getElementById(url)).text();
          $(document.getElementById(url)).text(content);
          return;
        }
        for (var i = 0; i < results.length; i++) {
          var object = results[i];
          var content = object.get('time') + ' ' + $(document.getElementById(url)).text();
          $(document.getElementById(url)).text(content);
        }
      },
      error: function(object, error) {
        console.log("Error: " + error.code + " " + error.message);
      }
    });

  });
}

function addCount(Counter) {
  var Counter = AV.Object.extend("Counter");
  url = $(".leancloud_visitors").attr('id').trim();
  title = $(".leancloud_visitors").attr('data-flag-title').trim();
  var query = new AV.Query(Counter);
  query.equalTo("url", url);
  query.find({
    success: function(results) {
      if (results.length > 0) {
        var counter = results[0];
        counter.fetchWhenSave(true);
        counter.increment("time");
        counter.save(null, {
          success: function(counter) {
            var content =  counter.get('time') + ' ' + $(document.getElementById(url)).text();
            $(document.getElementById(url)).text(content);
          },
          error: function(counter, error) {
            console.log('Failed to save Visitor num, with error message: ' + error.message);
          }
        });
      } else {
        var newcounter = new Counter();
        newcounter.set("title", title);
        newcounter.set("url", url);
        newcounter.set("time", 1);
        newcounter.save(null, {
          success: function(newcounter) {
              console.log("newcounter.get('time')="+newcounter.get('time'));
            var content = newcounter.get('time') + ' ' + $(document.getElementById(url)).text();
            $(document.getElementById(url)).text(content);
          },
          error: function(newcounter, error) {
            console.log('Failed to create');
          }
        });
      }
    },
    error: function(error) {
      console.log('Error:' + error.code + " " + error.message);
    }
  });
}
$(function() {
  var Counter = AV.Object.extend("Counter");
  if ($('.leancloud_visitors').length == 1) {
    addCount(Counter);
  } else if ($('.post-title-link').length > 1) {
    showTime(Counter);
  }
}); 
</script>

  
</body>
</html>
